package com.hnkywl.sts.dao.organ;

import com.hnkywl.sts.entity.organ.Khh;
import net.ruixin.orm.hibernate.Page;
import net.ruixin.orm.hibernate.SimpleHibernateTemplate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.hibernate.tool.hbm2x.StringUtils;
import org.hibernate.transform.AliasToEntityMapResultTransformer;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-23
 * Time: 下午1:37
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class KhhDao extends SimpleHibernateTemplate<Khh, Long> {
//    public Page<Khh> findKhhList(Long nbgsId, String qc,String jc, int pageNo, int pageSize) {
//        Page<Khh> page = new Page<Khh>();
//        page.setAutoCount(true);
//        page.setPageNo(pageNo);
//        page.setPageSize(pageSize);
//        page.setOrder("desc");
//        page.setOrderBy("lastUpdated");
//        if (nbgsId != null) {
//            return this.findByCriteria(page, new Criterion[]{Restrictions.eq("nbgs.id", nbgsId)});
//        }  else if (StringUtils.isNotEmpty(qc)) {
//            return this.findByCriteria(page, new Criterion[]{Restrictions.like("qc", qc, MatchMode.START)});
//        }  else if (StringUtils.isNotEmpty(jc)) {
//            return this.findByCriteria(page, new Criterion[]{Restrictions.like("jc", jc,MatchMode.START)});
//        } else
//            return this.findAll(page);
//    }
//


    public Page<Khh> findKhhList(Long nbgsId, String qc,String jc, int pageNo, int pageSize) {
        String hql = null;
        List<Object> param = new ArrayList<Object>();
        StringBuilder condition = new StringBuilder();
        Page<Khh> page = new Page<Khh>(pageNo, pageSize, 0);
        //==============条件拼接===============
        if (qc != null && !qc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" qc like ? ");
            param.add("%"+qc.trim() + "%");
        }
        if (jc != null && !jc.isEmpty()) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" jc like ? ");
            param.add("%"+jc.trim() + "%");
        }
        if (nbgsId != null) {
            if (condition.length() != 0)
                condition.append(" and ");
            condition.append(" nbgs.id = ? ");
            param.add(nbgsId);
        }
        //==============记录数量===============
        hql = "select count(*) from Khh where qyjg.id=null";
        if (condition.length() != 0)
            hql = hql + " and " + condition;
        Long c = null;
        if (param.size() != 0) {
            c = (Long) findUnique(hql, param.toArray());
            page.setTotalCount(c.intValue());
        } else {
            c = (Long) findUnique(hql);
            page.setTotalCount(c.intValue());
        }
        if (c == 0)
            return page;
        //==============查询结果===============
        hql = "from Khh where qyjg.id=null";
        if (condition.length() != 0)
            hql = hql + " and " + condition +  "order by lastUpdated desc";
        if (param.size() != 0)
            return find(page, hql, param.toArray());
        return find(page, hql);
    }



    public Page<Khh> findKhhList(String khhmc,Long nbgsId, int pageNo, int pageSize) {
        Page<Khh> page = new Page<Khh>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("lastUpdated");
        if (StringUtils.isNotEmpty(khhmc)) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.like("khhmc", khhmc, MatchMode.START)});
        }else if (nbgsId != null) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.eq("nbgs.id", nbgsId)});
        } else {
            return this.findAll(page);
        }
    }

    public Page<Khh> findNbgsKhhList(Long nbgsId, int pageNo, int pageSize) {
        Page<Khh> page = new Page<Khh>();
        page.setAutoCount(true);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        page.setOrder("desc");
        page.setOrderBy("lastUpdated");
        if (nbgsId != null) {
            return this.findByCriteria(page, new Criterion[]{Restrictions.eq("nbgs.id", nbgsId)});
        } else {
            return this.findAll(page);
        }
    }


    /**
     * 判断岗位属性是否重复
     * @param id       岗位id
     * @param propertyValue       岗位属性值
     * @param propertyName       岗位属性名
     * @return
     */
    public Boolean checkUnique(Long id,Long nbgsId, Object propertyValue,String propertyName) {
        String hql;
        if (propertyValue == null)
            return false;
        if(id == null){
            //修改检验
            hql = "from Khh where " + propertyName + " = ? and nbgs.id = ? ";
            return this.existsQuery(hql, propertyValue,nbgsId);
        }else{
            //新增检验
            hql = "from Khh where " + propertyName + " = ? and id <> ?  and nbgs.id = ?";
            return this.existsQuery(hql, propertyValue,id,nbgsId);
        }
    }

    /**
     * 获取同层次的当前最大的排序号
     *
     * @param nbgsId 公司机构的Id
     * @return
     */
    public Long findMaxPxh(Long nbgsId) {
        String hql = "select MAX(pxh) from Khh where nbgs.id = ?";
        return findLong(hql, nbgsId);
    }



    public Khh findKhhbyZh(String zh){
        String hql = " from Khh where zh = ? ";
        return  (Khh)find(hql,zh).get(0);
    }

    public Khh findKhhbyGsId(Long gsId){
        String hql = " from Khh where nbgs.id = ? ";
        return find(hql,gsId).size()==0?null:(Khh)find(hql,gsId).get(0);
    }

    public List<Map> findKhhByEjzhId(Long ejzhId) {
        String sql = "    select distinct khh.id as \"khh.id\",khh.qc as \"khh.jc\" " +
                "        from t_sys_khh  khh " +
                "        left join t_sys_ejzh ejzh on ejzh.khh_id = khh.id " +
                "          where khh.deleted = 0 and ejzh.id = ? ";
        return (List<Map>) this.createSQLQuery(sql, ejzhId).setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE).list();
    }

    public List<Map> findKhhByNbgsId(Long ejzhId) {
        String sql = "    select distinct khh.id as \"khh.id\",khh.qc as \"khh.jc\" " +
                "        from t_sys_khh  khh " +
                "          where khh.deleted = 0 and khh.nbgs_id = ? ";
        return (List<Map>) this.createSQLQuery(sql, ejzhId).setResultTransformer(AliasToEntityMapResultTransformer.INSTANCE).list();
    }

}
